< previous page page_444 next page >

Page 444
Table 19.3 Visual Basic's Built-In Color Constants
ConstantNumeric Value (Decimal)
vbBlack0
vbRed255
vbGreen65280
vbYellow65535
vbBlue16711680
vbMagenta16711935
vbCyan16776960
vbWhite16777215

note.gif System color constants are also available. For example, the following statement sets the color of a form to the same color as the Windows desktop:

Form1.BackColor = vbDesktop n
The color constants use such unusual numbers because each number represents the levels of red, green, and blue in a given color. If you want to create a custom color and know how much of each primary color to include, you can find out the color number by using the RGB function. Each primary color can have a setting between 0 and 255. For example, the following code determines the number for the color gray (equal parts red, green, and blue):
Dim lNewColor As Long
lNewColor = RGB(192,192,192)
Form1.BackColor = lNewColor
note.gif An easy way to find out a color number for use in code is to select the color from the Color list and make a note of the number displayed in the Properties window. square.gif

Another useful color function is the QBColor function. In earlier versions of BASIC, such as QuickBasic and GWBasic for MS-DOS, consecutive integer numbers represented colors. The QBColor function returns the hexadecimal RGB equivalent color. This capability is useful if you want to generate random colors, as in the following example:
Private Sub Form_Click()

    Dim lRGBColor As Long
    Dim nIntColor As Integer

    'Choose random number between 0 and 15
    nIntColor = Int(16 * Rnd)
    lRGBColor = QBColor(nIntColor)
    Me.BackColor = lRGBColor

End Sub

 
< previous page page_444 next page >